package com.project.service.base.CrpData;

import com.project.entity.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.UnsupportedEncodingException;


/**
 * author: WheelChen
 * data: 2017/7/17
 * function:
 */
//让checked例外也回滚
@Transactional(rollbackFor = Exception.class)
@Service
public class CrypDataHelperImpl extends CryBaseData implements CrypDataHelper {
    /**
     * author: WheelChen
     * date: 2017/7/17
     * function: 加密邮箱
     *
     * @param email 邮箱
     * @return
     */
    @Override
    public String encryptEmail(String email) throws Exception {
        //待加密数据
        String data = email.substring(0, email.indexOf("@"));
        //邮箱类型
        String type = email.substring(email.indexOf("@"), email.length());
        return encry(data) + type;

    }

    /**
     * author: WheelChen
     * date: 2017/7/17
     * function: 加密邮箱
     *
     * @param mobilePhone 手机号
     * @return
     */
    @Override
    public String encryptMobilePhone(String mobilePhone) throws Exception {
        //保存数据
        String prefix = mobilePhone.substring(0, 3);
        String suffix = mobilePhone.substring(mobilePhone.length() - 4);

        //待加密数据
        String data = mobilePhone.substring(3, 7);
        return prefix + encry(data) + suffix;
    }

    /**
     * author: WheelChen
     * date: 2017/7/18
     * function: 加密真实姓名
     *
     * @param realName 真实姓名
     * @return
     */
    @Override
    public String encryptRealName(String realName) throws Exception {
        String encryName;
        if (realName.length() > 1) {
            //姓 （汉字第一字）
            String lastName = realName.substring(0, 1);
            String data = realName.substring(1, realName.length());
            encryName = lastName + encry(data);
        } else
            encryName = realName;
        return encryName;
    }

    /**
     * author: WheelChen
     * date: 2017/7/18
     * function: 加密所有数据
     *
     * @param data
     * @return
     */
    @Override
    public String encryptAll(String data) throws Exception {
        return encry(data);
    }


    /**
     * author: WheelChen
     * date: 2017/7/17
     * function: 解密邮箱
     *
     * @param email 邮箱
     * @return
     */
    @Override
    public String decEmail(String email) throws Exception {
        //待解密数据
        String data = email.substring(0, email.indexOf("@"));
        //邮箱类型
        String type = email.substring(email.indexOf("@"), email.length());
        return dec(data) + type;
    }

    /**
     * author: WheelChen
     * date: 2017/7/17
     * function: 解密手机号
     *
     * @param mobilePhone 手机号
     * @return
     */
    @Override
    public String decMobilePhone(String mobilePhone) throws Exception {
        //保存数据
        String Prefix = mobilePhone.substring(0, 3);
        String suffix = mobilePhone.substring(mobilePhone.length() - 4);

        //待加密数据
        String data = mobilePhone.substring(3, mobilePhone.length() - 4);
        System.out.println(data);
        return Prefix + dec(data) + suffix;
    }

    /**
     * author: WheelChen
     * date: 2017/7/18
     * function: 解密真实姓名
     *
     * @param encryName 姓名密文
     * @return
     */
    @Override
    public String decRealName(String encryName) throws Exception {
        String dectName = "";
        if (StringUtils.isEmpty(encryName))
            throw new Exception("待解密姓名为空");
        if (encryName.length() > 1) {
            //姓 （汉字第一字）
            String lastName = encryName.substring(0, 1);
            String data = encryName.substring(1, encryName.length());
            dectName = lastName + dec(data);
        }
        return dectName;
    }

    /**
     * author: WheelChen
     * date: 2017/7/18
     * function: 解密所有数据
     *
     * @param data
     * @return
     */
    @Override
    public String decAll(String data) throws UnsupportedEncodingException {
        return dec(data);

    }

    /**
     * author: WheelChen
     * date: 2017/7/17
     * function: 加密用户数据
     *
     * @param user
     * @return
     */
    @Override
    public void encryptUser(User user) throws Exception {
        if (StringUtils.isNotEmpty(user.getEmail()))
            user.setEmail(encryptEmail(user.getEmail()));
        if (StringUtils.isNotEmpty(user.getMobilephone()))
            user.setMobilephone(encryptMobilePhone(user.getMobilephone()));
        if (StringUtils.isNotEmpty(user.getQq()))
            user.setQq(encryptAll(user.getQq()));
        if (StringUtils.isNotEmpty(user.getWechat()))
            user.setWechat(encryptAll(user.getWechat()));
        if (StringUtils.isNotEmpty(user.getRealname()))
            user.setRealname(encryptRealName(user.getRealname()));
    }

    /**
     * author: WheelChen
     * date: 2017/7/18
     * function: 解密用户
     *
     * @param user
     * @return
     */
    @Override
    public User decUser(User user) throws Exception {
        //对象克隆防止重复加解密对象
        User user1 = (User) user.clone();

        if (StringUtils.isNotEmpty(user.getEmail()))
            user1.setEmail(decEmail(user.getEmail()));
        if (StringUtils.isNotEmpty(user.getMobilephone()))
            user1.setMobilephone(decMobilePhone(user.getMobilephone()));
        if (StringUtils.isNotEmpty(user.getQq()))
            user1.setQq(decAll(user.getQq()));
        if (StringUtils.isNotEmpty(user.getWechat()))
            user1.setWechat(decAll(user.getWechat()));
        if (StringUtils.isNotEmpty(user.getRealname()))
            user1.setRealname(decRealName(user.getRealname()));

        return user1;
    }
}
